home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9046 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Date: 7 Mar 96 15:09:47 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.826211387@rscernix>
  8. References: <4g0elg$mdr@redstone.interpath.net> <4h0bbo$c0q@fohnix.metronet.com> <4h7ce0$ojd@news.interpath.net> <4h85sm$lm0@texas.nwlink.com> <danpop.825961872@rscernix> <313C5CD1.1209@oc.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <313C5CD1.1209@oc.com> Larry Weiss <lfw@oc.com> writes:
  13.  
  14. >Dan Pop wrote:
  15. >> 
  16. >> In <4h85sm$lm0@texas.nwlink.com> Teresa Reiko <tjr19@mail.nwlink.com> writes:
  17. >> >Here are the three worst Microsoft coding rules, in my opinion at least:
  18. >> >Every line of code must contain a comment.
  19. >> >Any number greater than 2 must be a named constant.
  20. >> >No procedure may be longer than 25 lines.
  21. >
  22. >> Actually, the second rule is quite good (except that '2' should be replaced
  23. >> by '1' :-)  Magic constants embedded in the code are a huge nuisance
  24. >> for maintenability.  If you have to change an explicit (aka magic) constant
  25. >> in a piece of code, how can you be sure that you aren't going to break
  26. >> anything?  By changing a macro definition, you _know_ that you won't
  27. >> break anything, because all the dependencies of that macro will be
  28. >> automatically taken care of by the compiler (and your "make" utility). 
  29. >
  30. >I agree that there are good uses for giving a literal a name, and using
  31. >that name instead of repeated usage of the literal, but to make a
  32. >mandate that even if the literal were referenced only once you must give
  33. >it a name would seem too extreme.
  34.  
  35. The point is that software is not usually cast in concrete.  What you
  36. thought to be a one-time magic constant might be used later (in a
  37. future release/version of the program) in another place and, if you
  38. don't follow the rule, chances are that it will be used again
  39.  
  40. >In the environment of named literals, I must always refer back to the 
  41. >mapping. 
  42.  
  43. Not always, only when the actual value is of any interest.  In the vast
  44. majority of cases, it isn't.  Finding a macro definition with grep is
  45. trivial, you can even put a wrapper around grep.
  46.  
  47. >Now you would say, fine, just deal with the abstraction and
  48. >not the actual value, but what I lose is the ability to readily make
  49. >comparisons to other literal values when they just have names.   I can't
  50. >readily see the proportions and other relative associations that are
  51. >usually there in the code.   I no longer have an immediate grasp of
  52. >the qualities of "small" or "large"  or maybe "unusually large" if all
  53. >is hidden in an abstract, one string fits all name.   
  54.  
  55. More often than not, this is irrelevant to the maintenance of a software
  56. package.  When it isn't, see my previous paragraph.
  57.  
  58. >When there are errors
  59. >in the #define of the literal's name, these can be very hard to detect,
  60. >as they are hidden away in the header file.   
  61.  
  62. They're as easy to detect as errors in magic constants.  Many compilers
  63. will even display the line as it looks after preprocessing, to help you.
  64. Anyway, whenever I use a macro definition which is not trivial, I test
  65. its correctness separately, to be sure that it is OK.  But we were talking
  66. about trivial macro definitions here, where a plain constant is put behind
  67. a (symbolic and hopefully meaningful) name.
  68.  
  69. A well chosen name says a lot more to the programmer and maintainer than
  70. an arbitrary constant.
  71.  
  72. >Anyway, I'm just saying teach the concept of named literals and then let
  73. >the programmer make the decision on a case by case basis.
  74.  
  75. This is not how coding rules/standards work :-)
  76.  
  77. Dan
  78. --
  79. Dan Pop
  80. CERN, CN Division
  81. Email: danpop@mail.cern.ch 
  82. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  83.